package com.adobe.captivate.enums
{
	import flash.utils.describeType;
	
	/* Source - http://scottbilas.com/2008/12/16/update-2-faking-enums-in-as3/ */

	public class CPBaseEnum
	{
		public function get Name() :String
			{ return _name; }

		public function toString() :String // override
			{ return Name; }

		protected static function initEnum(i_type :*) :void
		{
			var type :XML = flash.utils.describeType(i_type);
			for each (var constant :XML in type.constant)
			{
				var enumConstant :CPBaseEnum = i_type[constant.@name];

				// if 'text' is already initialized, then we're probably
				// calling initEnum() on the same type twice by accident,
				// likely a copy-paste bonehead mistake.
				if (enumConstant.Name != null)
				{
					throw new Error("Can't initialize '" + i_type + "' twice");
				}

				// if the types don't match then probably have another
				// copy-paste error.
				var enumConstantObj :* = enumConstant;
				if (enumConstantObj.constructor != i_type)
				{
					throw new Error(
						"Constant type '" + enumConstantObj.constructor + "' " +
						"does not match its enum class '" + i_type + "'");
				}

				enumConstant._name = constant.@name;
			}
		}
		private var _name :String = null;
	}
}